//===============================================================================
// Microsoft patterns & practices Enterprise Library Contribution
// Resource Application Block
//===============================================================================

using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration.ObjectBuilder;
using Microsoft.Practices.EnterpriseLibrary.Common.Instrumentation;
using Microsoft.Practices.ObjectBuilder;
using EntLibContrib.Resource;
using EntLibContrib.Resource.Configuration;
using EntLibContrib.Resource.Instrumentation;
#if !NUNIT
using Microsoft.VisualStudio.TestTools.UnitTesting;
#else
using NUnit.Framework;
using TestClass = NUnit.Framework.TestFixtureAttribute;
using TestInitialize = NUnit.Framework.SetUpAttribute;
using TestCleanup = NUnit.Framework.TearDownAttribute;
using TestMethod = NUnit.Framework.TestAttribute;
#endif


namespace EntLibContrib.Resource.Tests
{
	[TestClass]
	public class InternalResourceManagerCustomFactoryFixture
	{
		private string testString = "BlockName";
		private string testValue = "Enterprise Library Resource Application Block";

		[TestMethod]
		public void CheckResourceManagerCustomFactoryInstanceTest ()
		{
			ResourceManagerCustomFactory resourceManagerCustomFactory = new ResourceManagerCustomFactory();
			Assert.IsNotNull(resourceManagerCustomFactory);
		}

		[TestMethod]
		public void GetResourceManagerCustomFactoryConfigurationTest ()
		{
			TestResourceManagerCustomFactory testResourceManagerCustomFactory = new TestResourceManagerCustomFactory();
			ResourceManagerData resourceManagerData = testResourceManagerCustomFactory.GetConfigurationData("InternalResourceManager", new InternalResourceConfigurationSource());
			Assert.IsNotNull(resourceManagerData);
			Assert.AreEqual("InternalResourceManager", resourceManagerData.Name);
			Assert.AreEqual("InternalResourceProvider", resourceManagerData.ResourceProvider);
		}

		[TestMethod]
		public void GetResourceManagerCustomFactoryInstumentationProviderTest ()
		{
			TestResourceManagerCustomFactory testResourceManagerCustomFactory = new TestResourceManagerCustomFactory();
			ResourceInstrumentationProvider resourceInstrumentationProvider = testResourceManagerCustomFactory.CreateResourceInstrumentationProvider("InternalResourceManager", new InternalResourceConfigurationSource(), new ConfigurationReflectionCache());
			Assert.IsNotNull(resourceInstrumentationProvider);
		}

		[TestMethod]
		public void GetResourceManagerCustomFactoryBuildResourceManagerTest ()
		{
			TestResourceManagerCustomFactory testResourceManagerCustomFactory = new TestResourceManagerCustomFactory();
			IConfigurationSource configurationSource = new InternalResourceConfigurationSource();
			ConfigurationReflectionCache reflectionCache = new ConfigurationReflectionCache();
			ResourceManagerData resourceManagerData = testResourceManagerCustomFactory.GetConfigurationData("InternalResourceManager", configurationSource);
			IBuilderContext context = new BuilderContext(new BuilderStrategyChain(), new Locator(), new PolicyList());
			IResourceProvider resourceProvider = ResourceProviderCustomFactory.Instance.Create(context, resourceManagerData.ResourceProvider, configurationSource, reflectionCache);
			ResourceInstrumentationProvider resourceInstrumentationProvider = testResourceManagerCustomFactory.CreateResourceInstrumentationProvider("InternalResourceManager", configurationSource, reflectionCache);
			ResourceManager createdObject = new ResourceManagerFactoryHelper().BuildResourceManager(resourceManagerData.Name, resourceProvider, resourceInstrumentationProvider);
			Assert.IsNotNull(createdObject);
			Assert.AreEqual(testValue, createdObject[testString]);
		}

		[TestMethod]
		public void GetResourceManagerCustomFactoryCreateObjectTest ()
		{
			TestResourceManagerCustomFactory testResourceManagerCustomFactory = new TestResourceManagerCustomFactory();
			IConfigurationSource configurationSource = new InternalResourceConfigurationSource();
			ConfigurationReflectionCache reflectionCache = new ConfigurationReflectionCache();
			IBuilderContext context = new BuilderContext(new BuilderStrategyChain(), new Locator(), new PolicyList());
			object resourceManager = testResourceManagerCustomFactory.CreateObject(context, "InternalResourceManager", configurationSource, reflectionCache);
			Assert.IsNotNull(resourceManager);
#if NUNIT
			Assert.IsInstanceOfType(typeof(ResourceManager), resourceManager);
#else
			Assert.IsInstanceOfType(resourceManager, typeof(ResourceManager));
#endif
			Assert.AreEqual(testValue, ((ResourceManager)resourceManager)[testString]);
		}
	}

	internal class TestResourceManagerCustomFactory : ResourceManagerCustomFactory
	{
		public TestResourceManagerCustomFactory ()
		{
		}

		public ResourceManagerData GetConfigurationData (string name, IConfigurationSource configurationSource)
		{
			return base.GetConfiguration(name, configurationSource);
		}

		public ResourceInstrumentationProvider CreateResourceInstrumentationProvider (string name, IConfigurationSource configurationSource, ConfigurationReflectionCache reflectionCache)
		{
			return CreateInstrumentationProvider(name, configurationSource, reflectionCache);
		}
	}
}